import 'package:flutter/material.dart';
import 'package:sow_app_core/sow_app_core.dart' show Presenter, ServerException;

mixin StatePresenter on Presenter {
  late BuildContext presenterContext;
  late void Function(VoidCallback) presenterSetState;
  @override
  void Function() get onComplete => () => presenterSetState(() {});

  @override
  void Function(Exception e) get onError => (e) {
        if (e is ServerException) {
          ScaffoldMessenger.of(presenterContext)
              .showSnackBar(SnackBar(content: Text(e.msg)));
        } else {
          ScaffoldMessenger.of(presenterContext)
              .showSnackBar(SnackBar(content: Text("客户端异常：${e.runtimeType}")));
          throw e;
        }
      };

  void Function() get onPop => () {
        Navigator.pop(presenterContext, true);
      };
}
